require(["farm-footer",'loading'], function(loading) {
	var removeDiv = '';
	var params = getUrlParams();
	var type = ''; 
    type = params.type?params.type:'';
	if(type==2){
	   	$('#ck_btn').find('span').addClass('sdd_tabSelected');
	   	$('#ck_list').show();
	   	$('#fb_btn').find('span').removeClass('sdd_tabSelected');
        $('#fb_list').hide();
	}
    //tab切换
    $('#fb_btn').on('click', function(){
    	var ifActive = $(this).find('span').attr('class');
        if(ifActive == 'sdd_tabSelected'){
            return false;
        } else {
            $('#fb_btn').find('span').addClass('sdd_tabSelected');
            $('#fb_list').show();
            $('#ck_btn').find('span').removeClass('sdd_tabSelected');
            $('#ck_list').hide();
        }
    });
    $('#ck_btn').on('click', function(){
    	var ifActive = $(this).find('span').attr('class');
        if(ifActive == 'sdd_tabSelected'){
            return false;
        } else {
            $('#ck_btn').find('span').addClass('sdd_tabSelected');
            $('#ck_list').show();
            $('#fb_btn').find('span').removeClass('sdd_tabSelected');
            $('#fb_list').hide();
        }
    });
    function func(){
    	alert(1111)
    }
	//创建vue实例
    var saleManage = new Vue({
        el: '#saleManage',
        data: {
            fbList:[],
            ckList:[],
            fbListLength: 0,
            ckListLength: 0,
            fbNoData: false,
            ckNoData:false,
            showLoading: false,
            listUrl: wxUrl + '/v3.3/goods/goodsList',
            frameUrl: wxUrl + '/v3.3/goods/soldOut',
            delUrl: wxUrl + '/v3.3/goods/delGoods',
            fbItem: {
                params:{
                    token: token,
                    userToken: userToken,
                    type: 1
                }
            },
            ckItem: {
                params:{
                    token: token,
                    userToken: userToken,
                    type: 0
                }
            },
            on_off_Item: {
                params:{
                    token: token,
                    userToken: userToken,
                    type: 1,
                    goodsId: ''
                }
            },
            delItem: {
                params:{
                    token: token,
                    userToken: userToken,
                    goodsId: ''
                }
            }
        },
        ready: function() {
            this.get_fblist();
            setTimeout(this.get_cklist(),600);
        },
        methods: {
            get_fblist:function(){
                this.$http.get(this.listUrl, this.fbItem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        var list = result.data.goodsList;
                        this.fbListLength = list.length;
                        if(list.length > 0){
                        	this.$set('fbList', list);
                        	this.fbNoData = false;
                        }else{
                        	this.fbNoData = true;
                        }
                    },function(response) {
                        //errorCallback
                    });
            },
            get_cklist:function(){
                this.$http.get(this.listUrl, this.ckItem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        var list = result.data.goodsList;
                        this.ckListLength = list.length;
                        for(var i in list){
                        	var goodsStatus = list[i].goodsStatus;
                        	if(goodsStatus == 0){
                        		list[i].goodsStatus = '待审核';
                        	}else if(goodsStatus == -1){
                        		list[i].goodsStatus = '审核失败';
                        	}else if(goodsStatus == 1){
                        		list[i].goodsStatus = '显示中';
                        	}else if(goodsStatus == -2){
                        		list[i].goodsStatus = '已下架';
                        	}else if(goodsStatus == -3){
                        		list[i].goodsStatus = '违规下架';
                        	}
                        }
                        
                        if(list.length > 0){
                        	this.$set('ckList', list);
                        	this.ckNoData = false;
                        }else{
                        	this.ckNoData = true;
                        }
                        
                    },function(response) {
                        //errorCallback
                    });
            },
            onFrame:function(e){
            	this.on_off_Item.params.type = 1; //上架
            	this.on_off_Item.params.goodsId = $(e.currentTarget).attr('id');
            	removeDiv = $(e.currentTarget).parents('.sdd_product');
            	dialog('确定上架该商品？', this.shangjia);
            },
            shangjia: function(){
            	this.$http.get(this.frameUrl, this.on_off_Item)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.data.flag){
                        	promptBox('商品已上架！');
                        	this.get_fblist();
                        	this.get_cklist();
                        	removeDiv.hide(); //移除当前商品
                        	removeDiv='';
                        }
                    },function(response) {
                        //errorCallback
                    });
            },
            offFrame:function(e){
            	this.on_off_Item.params.type = 2; //下架
            	this.on_off_Item.params.goodsId = $(e.currentTarget).attr('id');
            	removeDiv = $(e.currentTarget).parents('.sdd_product');
                dialog('确定下架该商品？', this.xiajia);
            },
            xiajia: function(){
            	this.$http.get(this.frameUrl, this.on_off_Item)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.data.flag){
                        	promptBox('商品已下架！');
                        	this.get_fblist();
                        	this.get_cklist();
                        	removeDiv.hide(); //移除当前商品
                        	removeDiv='';
                        }
                    },function(response) {
                        //errorCallback
                    });
            },
            deleteGoods:function(e){
            	this.delItem.params.goodsId = $(e.currentTarget).attr('id');
            	removeDiv = $(e.currentTarget).parents('.sdd_product');
            	dialog('确定删除该商品？', this.shanchu);
            },
            shanchu:function(){
            	this.$http.get(this.delUrl, this.delItem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.data.flag){
                        	promptBox('商品已删除！');
                        	this.get_cklist();
                        	removeDiv.hide(); //移除当前商品
                        	removeDiv = '';
                        }
                    },function(response) {
                        //errorCallback
                    });
            }
        }
    });
    // 为可控制返回指定页面所有添加pushState
	history.pushState({}, document.title, null);
	pushVHistory("./sales_index.html");
	window.addEventListener("popstate", function() {
	    // 跳转到批发商品管理列表页面
		goBackVHistory();
	});
});
